<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python CGI编程</h1>

        <h2>什么是CGI</h2>
        <p>CGI 目前由NCSA维护，NCSA定义CGI如下：</p><p>
        CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如：HTTP服务器，提供同客户端HTML页面的接口。</p>

        <h2>网页浏览</h2>
        <p>为了更好的了解CGI是如何工作的，我们可以从在网页上点击一个链接或URL的流程：</p>
        <ul>
            <li>
                1、使用你的浏览器访问URL并连接到HTTP web 服务器。</li><li>
            2、Web服务器接收到请求信息后会解析URL，并查找访问的文件在服务器上是否存在，如果存在返回文件的内容，否则返回错误信息。</li><li>
            3、浏览器从服务器上接收信息，并显示接收的文件或者错误信息。</li></ul>

        <p>CGI程序可以是Python脚本，PERL脚本，SHELL脚本，C或者C++程序等。</p>

        <h2>
            CGI架构图
        </h2>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/11/Cgi01.png" alt="cgiarch" width="411" height="473" class="alignnone size-full wp-image-4561"></p>

        <h2>Web服务器支持及配置</h2>
        <p>在你进行CGI编程前，确保您的Web服务器支持CGI及已经配置了CGI的处理程序。</p>
        <p>Apache 支持CGI 配置：</p>
        <p>设置好CGI目录：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">ScriptAlias</span><span class="pln"> </span><span class="pun">/</span><span class="pln">cgi</span><span class="pun">-</span><span class="pln">bin</span><span class="str">/ /</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">cgi</span><span class="pun">-</span><span class="pln">bin</span><span class="pun">/</span></pre>

        <p>
            所有的HTTP服务器执行CGI程序都保存在一个预先配置的目录。这个目录被称为CGI目录，并按照惯例，它被命名为/var/www/cgi-bin目录。</p><p>
        CGI文件的扩展名为.cgi，python也可以使用.py扩展名。</p><p>
        默认情况下，Linux服务器配置运行的cgi-bin目录中为/var/www。</p><p>
        如果你想指定其他运行CGI脚本的目录，可以修改httpd.conf配置文件，如下所示：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="tag">&lt;Directory</span><span class="pln"> </span><span class="atv">"/var/www/cgi-bin"</span><span class="tag">&gt;</span><span class="pln">
   AllowOverride None
   Options +ExecCGI
   Order allow,deny
   Allow from all
</span><span class="tag">&lt;/Directory&gt;</span></pre>
        <p>在 AddHandler 中添加 .py 后缀，这样我们就可以访问 .py 结尾的 python 脚本文件：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">AddHandler</span><span class="pln"> cgi</span><span class="pun">-</span><span class="pln">script </span><span class="pun">.</span><span class="pln">cgi </span><span class="pun">.</span><span class="pln">pl </span><span class="pun">.</span><span class="pln">py</span></pre>
        <h2>第一个CGI程序</h2>
        <p>我们使用Python创建第一个CGI程序，文件名为hello.py，文件位于/var/www/cgi-bin目录中，内容如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Content-type:text/html"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">()</span><span class="pln">                             </span><span class="com"># 空行，告诉服务器结束头部</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">'&lt;html&gt;'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">'&lt;head&gt;'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">'&lt;meta charset="utf-8"&gt;'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">'&lt;title&gt;Hello Word - 我的第一个 CGI 程序！&lt;/title&gt;'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">'&lt;/head&gt;'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">'&lt;body&gt;'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">'&lt;h2&gt;Hello Word! 我是来自菜鸟教程的第一CGI程序&lt;/h2&gt;'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">'&lt;/body&gt;'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">'&lt;/html&gt;'</span><span class="pun">)</span></pre>
        <p>文件保存后修改 hello.py，修改文件权限为 755：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">chmod </span><span class="lit">755</span><span class="pln"> hello</span><span class="pun">.</span><span class="pln">py </span></pre>
        <p>以上程序在浏览器访问显示结果如下：</p>
        <p>
            <img width="70%" src="http://www.runoob.com/wp-content/uploads/2013/11/3E82A06B-FE1F-49B9-969C-183FABD56363.jpg"></p>
        <p>这个的hello.py脚本是一个简单的Python脚本，脚本第一行的输出内容"Content-type:text/html"发送到浏览器并告知浏览器显示的内容类型为"text/html"。</p><p>用 print 输出一个空行用于告诉服务器结束头部信息。</p>
        <h2>HTTP头部</h2>
        <p>hello.py文件内容中的" Content-type:text/html"即为HTTP头部的一部分，它会发送给浏览器告诉浏览器文件的内容类型。</p>
        <p>HTTP头部的格式如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">HTTP </span><span class="pun">字段名:</span><span class="pln"> </span><span class="pun">字段内容</span></pre><p>
        例如：</p><pre class="prettyprint prettyprinted" style=""><span class="typ">Content</span><span class="pun">-</span><span class="pln">type</span><span class="pun">:</span><span class="pln"> text</span><span class="pun">/</span><span class="pln">html</span></pre>

        <p>以下表格介绍了CGI程序中HTTP头部经常使用的信息：</p>
        <table class="reference">
            <tbody><tr>
                <th style="width:30%">头</th><th>描述</th>
            </tr>
            <tr> <td>Content-type: </td><td>请求的与实体对应的MIME信息。例如: Content-type:text/html</td></tr>
            <tr> <td>Expires: Date </td><td>响应过期的日期和时间</td> </tr>
            <tr> <td>Location: URL </td><td>用来重定向接收方到非请求URL的位置来完成请求或标识新的资源</td> </tr>
            <tr> <td>Last-modified: Date</td><td>请求资源的最后修改时间</td> </tr>
            <tr> <td>Content-length: N</td><td>请求的内容长度</td> </tr>
            <tr> <td>Set-Cookie: String </td><td>设置Http Cookie</td> </tr>
            </tbody></table>

        <h2>CGI环境变量</h2>
        <p>所有的CGI程序都接收以下的环境变量，这些变量在CGI程序中发挥了重要的作用：</p>
        <table class="reference">
            <tbody><tr><th style="width:30%;">变量名</th><th>描述</th></tr>
            <tr> <td>CONTENT_TYPE</td><td>这个环境变量的值指示所传递来的信息的MIME类型。目前，环境变量CONTENT_TYPE一般都是：application/x-www-form-urlencoded,他表示数据来自于HTML表单。</td> </tr>
            <tr> <td>CONTENT_LENGTH</td><td>如果服务器与CGI程序信息的传递方式是POST，这个环境变量即使从标准输入STDIN中可以读到的有效数据的字节数。这个环境变量在读取所输入的数据时必须使用。</td> </tr>
            <tr> <td>HTTP_COOKIE</td><td>客户机内的 COOKIE 内容。</td> </tr>
            <tr> <td>HTTP_USER_AGENT</td><td>提供包含了版本数或其他专有数据的客户浏览器信息。</td> </tr>
            <tr> <td>PATH_INFO</td><td>这个环境变量的值表示紧接在CGI程序名之后的其他路径信息。它常常作为CGI程序的参数出现。</td> </tr>
            <tr> <td>QUERY_STRING</td><td>如果服务器与CGI程序信息的传递方式是GET，这个环境变量的值即使所传递的信息。这个信息经跟在CGI程序名的后面，两者中间用一个问号'?'分隔。</td> </tr>
            <tr> <td>REMOTE_ADDR</td><td>这个环境变量的值是发送请求的客户机的IP地址，例如上面的192.168.1.67。这个值总是存在的。而且它是Web客户机需要提供给Web服务器的唯一标识，可以在CGI程序中用它来区分不同的Web客户机。</td> </tr>
            <tr> <td>REMOTE_HOST</td><td>这个环境变量的值包含发送CGI请求的客户机的主机名。如果不支持你想查询，则无需定义此环境变量。</td> </tr>
            <tr> <td>REQUEST_METHOD</td><td>提供脚本被调用的方法。对于使用 HTTP/1.0 协议的脚本，仅 GET 和 POST 有意义。</td></tr>
            <tr> <td>SCRIPT_FILENAME</td><td>CGI脚本的完整路径</td></tr>
            <tr> <td>SCRIPT_NAME</td><td>CGI脚本的的名称</td></tr>
            <tr> <td>SERVER_NAME</td><td>这是你的 WEB 服务器的主机名、别名或IP地址。</td></tr>
            <tr> <td>SERVER_SOFTWARE</td><td>这个环境变量的值包含了调用CGI程序的HTTP服务器的名称和版本号。例如，上面的值为Apache/2.2.14(Unix)</td></tr>
            </tbody></table>
        <p>以下是一个简单的CGI脚本输出CGI的环境变量：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> os

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Content-type: text/html"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;meta charset=\"utf-8\"&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;b&gt;环境变量&lt;/b&gt;&lt;br&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;ul&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">for</span><span class="pln"> key </span><span class="kwd">in</span><span class="pln"> os</span><span class="pun">.</span><span class="pln">environ</span><span class="pun">.</span><span class="pln">keys</span><span class="pun">():</span><span class="pln">
    </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;li&gt;&lt;span style='color:green'&gt;%30s &lt;/span&gt; : %s &lt;/li&gt;"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="pun">(</span><span class="pln">key</span><span class="pun">,</span><span class="pln">os</span><span class="pun">.</span><span class="pln">environ</span><span class="pun">[</span><span class="pln">key</span><span class="pun">]))</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/ul&gt;"</span><span class="pun">)</span></pre>
        <p>将以上点保存为 test.py ,并修改文件权限为 755，执行结果如下：</p>
        <p><img width="70%" src="http://www.runoob.com/wp-content/uploads/2013/11/0B7EB575-8393-43A0-949A-E46DCFB840FE.jpg"></p>
        <h2>GET和POST方法</h2>
        <p>浏览器客户端通过两种方法向服务器传递信息，这两种方法就是 GET 方法和 POST 方法。</p>

        <h3>使用GET方法传输数据</h3>
        <p>GET方法发送编码后的用户信息到服务端，数据信息包含在请求页面的URL上，以"?"号分割, 如下所示：</p>

        <pre class="prettyprint prettyprinted" style=""><span class="pln">http</span><span class="pun">:</span><span class="com">//www.test.com/cgi-bin/hello.py?key1=value1&amp;key2=value2</span></pre>
        有关 GET 请求的其他一些注释：
        <ul>
            <li>GET 请求可被缓存</li>
            <li>GET 请求保留在浏览器历史记录中</li>
            <li>GET 请求可被收藏为书签</li>
            <li>GET 请求不应在处理敏感数据时使用</li>
            <li>GET 请求有长度限制</li>
            <li>GET 请求只应当用于取回数据</li>
        </ul>
        <h3>简单的url实例：GET方法</h3>
        <p>以下是一个简单的URL，使用GET方法向hello_get.py程序发送两个参数：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="str">/cgi-bin/</span><span class="pln">test</span><span class="pun">.</span><span class="pln">py</span><span class="pun">?</span><span class="pln">name</span><span class="pun">=菜鸟教程&amp;</span><span class="pln">url</span><span class="pun">=</span><span class="pln">http</span><span class="pun">:</span><span class="com">//www.runoob.com</span></pre>
        <p>以下为hello_get.py文件的代码：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># CGI处理模块</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> cgi</span><span class="pun">,</span><span class="pln"> cgitb

</span><span class="com"># 创建 FieldStorage 的实例化</span><span class="pln">
form </span><span class="pun">=</span><span class="pln"> cgi</span><span class="pun">.</span><span class="typ">FieldStorage</span><span class="pun">()</span><span class="pln">

</span><span class="com"># 获取数据</span><span class="pln">
site_name </span><span class="pun">=</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'name'</span><span class="pun">)</span><span class="pln">
site_url  </span><span class="pun">=</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'url'</span><span class="pun">)</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Content-type:text/html"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;html&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;meta charset=\"utf-8\"&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;title&gt;菜鸟教程 CGI 测试实例&lt;/title&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;h2&gt;%s官网：%s&lt;/h2&gt;"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="pun">(</span><span class="pln">site_name</span><span class="pun">,</span><span class="pln"> site_url</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/html&gt;"</span><span class="pun">)</span></pre>
        <p>文件保存后修改 hello_get.py，修改文件权限为 755：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">chmod </span><span class="lit">755</span><span class="pln"> hello_get</span><span class="pun">.</span><span class="pln">py </span></pre>
        <p>浏览器请求输出结果：</p>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/11/4C034008-B0B0-452F-AC97-C2BE37B9C7AF.jpg"></p>
        <h3>简单的表单实例：GET方法</h3>
        <p>以下是一个通过HTML的表单使用GET方法向服务器发送两个数据，提交的服务器脚本同样是hello_get.py文件，hello_get.html 代码如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="dec">&lt;!DOCTYPE html&gt;</span><span class="pln">
</span><span class="tag">&lt;html&gt;</span><span class="pln">
</span><span class="tag">&lt;head&gt;</span><span class="pln">
</span><span class="tag">&lt;meta</span><span class="pln"> </span><span class="atn">charset</span><span class="pun">=</span><span class="atv">"utf-8"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;title&gt;</span><span class="pln">菜鸟教程(runoob.com)</span><span class="tag">&lt;/title&gt;</span><span class="pln">
</span><span class="tag">&lt;/head&gt;</span><span class="pln">
</span><span class="tag">&lt;body&gt;</span><span class="pln">
</span><span class="tag">&lt;form</span><span class="pln"> </span><span class="atn">action</span><span class="pun">=</span><span class="atv">"/cgi-bin/hello_get.py"</span><span class="pln"> </span><span class="atn">method</span><span class="pun">=</span><span class="atv">"get"</span><span class="tag">&gt;</span><span class="pln">
站点名称: </span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"name"</span><span class="tag">&gt;</span><span class="pln">  </span><span class="tag">&lt;br</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">

站点 URL: </span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"url"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
</span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"submit"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"提交"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
</span><span class="tag">&lt;/form&gt;</span><span class="pln">
</span><span class="tag">&lt;/body&gt;</span><span class="pln">
</span><span class="tag">&lt;/html&gt;</span></pre><p>默认情况下 cgi-bin 目录只能存放脚本文件，我们将 hello_get.html 存储在 test 目录下，修改文件权限为 755：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">chmod </span><span class="lit">755</span><span class="pln"> hello_get</span><span class="pun">.</span><span class="pln">html</span></pre>
        <p>Gif 演示如下所示：</p>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/11/hello_get.gif"></p>
        <h3>使用POST方法传递数据</h3>
        <p>使用POST方法向服务器传递数据是更安全可靠的，像一些敏感信息如用户密码等需要使用POST传输数据。</p>
        <p>以下同样是hello_get.py ，它也可以处理浏览器提交的POST表单数据:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># CGI处理模块</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> cgi</span><span class="pun">,</span><span class="pln"> cgitb

</span><span class="com"># 创建 FieldStorage 的实例化</span><span class="pln">
form </span><span class="pun">=</span><span class="pln"> cgi</span><span class="pun">.</span><span class="typ">FieldStorage</span><span class="pun">()</span><span class="pln">

</span><span class="com"># 获取数据</span><span class="pln">
site_name </span><span class="pun">=</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'name'</span><span class="pun">)</span><span class="pln">
site_url  </span><span class="pun">=</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'url'</span><span class="pun">)</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Content-type:text/html"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;html&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;meta charset=\"utf-8\"&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;title&gt;菜鸟教程 CGI 测试实例&lt;/title&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;h2&gt;%s官网：%s&lt;/h2&gt;"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="pun">(</span><span class="pln">site_name</span><span class="pun">,</span><span class="pln"> site_url</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/html&gt;"</span><span class="pun">)</span></pre>
        <p>以下为表单通过POST方法（<strong>method="post"</strong>）向服务器脚本 hello_get.py 提交数据:</p>

        <pre class="prettyprint prettyprinted" style=""><span class="dec">&lt;!DOCTYPE html&gt;</span><span class="pln">
</span><span class="tag">&lt;html&gt;</span><span class="pln">
</span><span class="tag">&lt;head&gt;</span><span class="pln">
</span><span class="tag">&lt;meta</span><span class="pln"> </span><span class="atn">charset</span><span class="pun">=</span><span class="atv">"utf-8"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;title&gt;</span><span class="pln">菜鸟教程(runoob.com)</span><span class="tag">&lt;/title&gt;</span><span class="pln">
</span><span class="tag">&lt;/head&gt;</span><span class="pln">
</span><span class="tag">&lt;body&gt;</span><span class="pln">
</span><span class="tag">&lt;form</span><span class="pln"> </span><span class="atn">action</span><span class="pun">=</span><span class="atv">"/cgi-bin/hello_get.py"</span><span class="pln"> </span><span class="atn">method</span><span class="pun">=</span><span class="atv">"post"</span><span class="tag">&gt;</span><span class="pln">
站点名称: </span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"name"</span><span class="tag">&gt;</span><span class="pln">  </span><span class="tag">&lt;br</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">

站点 URL: </span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"url"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
</span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"submit"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"提交"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
</span><span class="tag">&lt;/form&gt;</span><span class="pln">
</span><span class="tag">&lt;/body&gt;</span><span class="pln">
</span><span class="tag">&lt;/html&gt;</span><span class="pln">
</span><span class="tag">&lt;/form&gt;</span></pre>
        <p>Gif 演示如下所示：</p>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/11/hello_post.gif"></p>
        <h3>通过CGI程序传递checkbox数据</h3>
        <p>checkbox用于提交一个或者多个选项数据，HTML代码如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="dec">&lt;!DOCTYPE html&gt;</span><span class="pln">
</span><span class="tag">&lt;html&gt;</span><span class="pln">
</span><span class="tag">&lt;head&gt;</span><span class="pln">
</span><span class="tag">&lt;meta</span><span class="pln"> </span><span class="atn">charset</span><span class="pun">=</span><span class="atv">"utf-8"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;title&gt;</span><span class="pln">菜鸟教程(runoob.com)</span><span class="tag">&lt;/title&gt;</span><span class="pln">
</span><span class="tag">&lt;/head&gt;</span><span class="pln">
</span><span class="tag">&lt;body&gt;</span><span class="pln">
</span><span class="tag">&lt;form</span><span class="pln"> </span><span class="atn">action</span><span class="pun">=</span><span class="atv">"/cgi-bin/checkbox.py"</span><span class="pln"> </span><span class="atn">method</span><span class="pun">=</span><span class="atv">"POST"</span><span class="pln"> </span><span class="atn">target</span><span class="pun">=</span><span class="atv">"_blank"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"checkbox"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"runoob"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"on"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln"> 菜鸟教程
</span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"checkbox"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"google"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"on"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln"> Google
</span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"submit"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"选择站点"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
</span><span class="tag">&lt;/form&gt;</span><span class="pln">
</span><span class="tag">&lt;/body&gt;</span><span class="pln">
</span><span class="tag">&lt;/html&gt;</span></pre>
        <p>以下为 checkbox.py 文件的代码：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 引入 CGI 处理模块 </span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> cgi</span><span class="pun">,</span><span class="pln"> cgitb

</span><span class="com"># 创建 FieldStorage的实例 </span><span class="pln">
form </span><span class="pun">=</span><span class="pln"> cgi</span><span class="pun">.</span><span class="typ">FieldStorage</span><span class="pun">()</span><span class="pln">

</span><span class="com"># 接收字段数据</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'google'</span><span class="pun">):</span><span class="pln">
   google_flag </span><span class="pun">=</span><span class="pln"> </span><span class="str">"是"</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
   google_flag </span><span class="pun">=</span><span class="pln"> </span><span class="str">"否"</span><span class="pln">

</span><span class="kwd">if</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'runoob'</span><span class="pun">):</span><span class="pln">
   runoob_flag </span><span class="pun">=</span><span class="pln"> </span><span class="str">"是"</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
   runoob_flag </span><span class="pun">=</span><span class="pln"> </span><span class="str">"否"</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Content-type:text/html"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;html&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;meta charset=\"utf-8\"&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;title&gt;菜鸟教程 CGI 测试实例&lt;/title&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;h2&gt; 菜鸟教程是否选择了 : %s&lt;/h2&gt;"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> runoob_flag</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;h2&gt; Google 是否选择了 : %s&lt;/h2&gt;"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> google_flag</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/html&gt;"</span><span class="pun">)</span></pre>
        <p>修改 checkbox.py 权限：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">chmod </span><span class="lit">755</span><span class="pln"> checkbox</span><span class="pun">.</span><span class="pln">py</span></pre>
        <p>浏览器访问 Gif 演示图：</p>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/11/checkbox.gif"></p>
        <h3>通过CGI程序传递Radio数据</h3>
        <p>Radio 只向服务器传递一个数据，HTML代码如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="dec">&lt;!DOCTYPE html&gt;</span><span class="pln">
</span><span class="tag">&lt;html&gt;</span><span class="pln">
</span><span class="tag">&lt;head&gt;</span><span class="pln">
</span><span class="tag">&lt;meta</span><span class="pln"> </span><span class="atn">charset</span><span class="pun">=</span><span class="atv">"utf-8"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;title&gt;</span><span class="pln">菜鸟教程(runoob.com)</span><span class="tag">&lt;/title&gt;</span><span class="pln">
</span><span class="tag">&lt;/head&gt;</span><span class="pln">
</span><span class="tag">&lt;body&gt;</span><span class="pln">
</span><span class="tag">&lt;form</span><span class="pln"> </span><span class="atn">action</span><span class="pun">=</span><span class="atv">"/cgi-bin/radiobutton.py"</span><span class="pln"> </span><span class="atn">method</span><span class="pun">=</span><span class="atv">"post"</span><span class="pln"> </span><span class="atn">target</span><span class="pun">=</span><span class="atv">"_blank"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"radio"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"site"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"runoob"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln"> 菜鸟教程
</span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"radio"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"site"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"google"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln"> Google
</span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"submit"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"提交"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
</span><span class="tag">&lt;/form&gt;</span><span class="pln">
</span><span class="tag">&lt;/body&gt;</span><span class="pln">
</span><span class="tag">&lt;/html&gt;</span></pre>
        <p>radiobutton.py 脚本代码如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 引入 CGI 处理模块 </span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> cgi</span><span class="pun">,</span><span class="pln"> cgitb

</span><span class="com"># 创建 FieldStorage的实例 </span><span class="pln">
form </span><span class="pun">=</span><span class="pln"> cgi</span><span class="pun">.</span><span class="typ">FieldStorage</span><span class="pun">()</span><span class="pln">

</span><span class="com"># 接收字段数据</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'site'</span><span class="pun">):</span><span class="pln">
   site </span><span class="pun">=</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'site'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
   site </span><span class="pun">=</span><span class="pln"> </span><span class="str">"提交数据为空"</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Content-type:text/html"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;html&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;meta charset=\"utf-8\"&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;title&gt;菜鸟教程 CGI 测试实例&lt;/title&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;h2&gt; 选中的网站是 %s&lt;/h2&gt;"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> site</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/html&gt;"</span><span class="pun">)</span></pre><p>修改 radiobutton.py 权限：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">chmod </span><span class="lit">755</span><span class="pln"> radiobutton</span><span class="pun">.</span><span class="pln">py</span></pre>
        <p>浏览器访问 Gif 演示图：</p>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/11/radiobutton.gif"></p>
        <h3>通过CGI程序传递 Textarea 数据</h3>
        <p>Textarea 向服务器传递多行数据，HTML代码如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="dec">&lt;!DOCTYPE html&gt;</span><span class="pln">
</span><span class="tag">&lt;html&gt;</span><span class="pln">
</span><span class="tag">&lt;head&gt;</span><span class="pln">
</span><span class="tag">&lt;meta</span><span class="pln"> </span><span class="atn">charset</span><span class="pun">=</span><span class="atv">"utf-8"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;title&gt;</span><span class="pln">菜鸟教程(runoob.com)</span><span class="tag">&lt;/title&gt;</span><span class="pln">
</span><span class="tag">&lt;/head&gt;</span><span class="pln">
</span><span class="tag">&lt;body&gt;</span><span class="pln">
</span><span class="tag">&lt;form</span><span class="pln"> </span><span class="atn">action</span><span class="pun">=</span><span class="atv">"/cgi-bin/textarea.py"</span><span class="pln"> </span><span class="atn">method</span><span class="pun">=</span><span class="atv">"post"</span><span class="pln"> </span><span class="atn">target</span><span class="pun">=</span><span class="atv">"_blank"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;textarea</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"textcontent"</span><span class="pln"> </span><span class="atn">cols</span><span class="pun">=</span><span class="atv">"40"</span><span class="pln"> </span><span class="atn">rows</span><span class="pun">=</span><span class="atv">"4"</span><span class="tag">&gt;</span><span class="pln">
在这里输入内容...
</span><span class="tag">&lt;/textarea&gt;</span><span class="pln">
</span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"submit"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"提交"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
</span><span class="tag">&lt;/form&gt;</span><span class="pln">
</span><span class="tag">&lt;/body&gt;</span><span class="pln">
</span><span class="tag">&lt;/html&gt;</span></pre>
        <p>textarea.py 脚本代码如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 引入 CGI 处理模块 </span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> cgi</span><span class="pun">,</span><span class="pln"> cgitb

</span><span class="com"># 创建 FieldStorage的实例 </span><span class="pln">
form </span><span class="pun">=</span><span class="pln"> cgi</span><span class="pun">.</span><span class="typ">FieldStorage</span><span class="pun">()</span><span class="pln">

</span><span class="com"># 接收字段数据</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'textcontent'</span><span class="pun">):</span><span class="pln">
   text_content </span><span class="pun">=</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'textcontent'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
   text_content </span><span class="pun">=</span><span class="pln"> </span><span class="str">"没有内容"</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Content-type:text/html"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;html&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;meta charset=\"utf-8\"&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;title&gt;菜鸟教程 CGI 测试实例&lt;/title&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;h2&gt; 输入的内容是：%s&lt;/h2&gt;"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> text_content</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/html&gt;"</span><span class="pun">)</span></pre><p>修改 textarea.py 权限：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">chmod </span><span class="lit">755</span><span class="pln"> textarea</span><span class="pun">.</span><span class="pln">py</span></pre>
        <p>浏览器访问 Gif 演示图：</p>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/11/textarea.gif"></p>
        <h3>通过CGI程序传递下拉数据。</h3>
        <p>HTML 下拉框代码如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="dec">&lt;!DOCTYPE html&gt;</span><span class="pln">
</span><span class="tag">&lt;html&gt;</span><span class="pln">
</span><span class="tag">&lt;head&gt;</span><span class="pln">
</span><span class="tag">&lt;meta</span><span class="pln"> </span><span class="atn">charset</span><span class="pun">=</span><span class="atv">"utf-8"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;title&gt;</span><span class="pln">菜鸟教程(runoob.com)</span><span class="tag">&lt;/title&gt;</span><span class="pln">
</span><span class="tag">&lt;/head&gt;</span><span class="pln">
</span><span class="tag">&lt;body&gt;</span><span class="pln">
</span><span class="tag">&lt;form</span><span class="pln"> </span><span class="atn">action</span><span class="pun">=</span><span class="atv">"/cgi-bin/dropdown.py"</span><span class="pln"> </span><span class="atn">method</span><span class="pun">=</span><span class="atv">"post"</span><span class="pln"> </span><span class="atn">target</span><span class="pun">=</span><span class="atv">"_blank"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;select</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"dropdown"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;option</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"runoob"</span><span class="pln"> </span><span class="atn">selected</span><span class="tag">&gt;</span><span class="pln">菜鸟教程</span><span class="tag">&lt;/option&gt;</span><span class="pln">
</span><span class="tag">&lt;option</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"google"</span><span class="tag">&gt;</span><span class="pln">Google</span><span class="tag">&lt;/option&gt;</span><span class="pln">
</span><span class="tag">&lt;/select&gt;</span><span class="pln">
</span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"submit"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"提交"</span><span class="tag">/&gt;</span><span class="pln">
</span><span class="tag">&lt;/form&gt;</span><span class="pln">
</span><span class="tag">&lt;/body&gt;</span><span class="pln">
</span><span class="tag">&lt;/html&gt;</span></pre>
        <p>dropdown.py 脚本代码如下所示：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 引入 CGI 处理模块 </span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> cgi</span><span class="pun">,</span><span class="pln"> cgitb

</span><span class="com"># 创建 FieldStorage的实例 </span><span class="pln">
form </span><span class="pun">=</span><span class="pln"> cgi</span><span class="pun">.</span><span class="typ">FieldStorage</span><span class="pun">()</span><span class="pln">

</span><span class="com"># 接收字段数据</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'dropdown'</span><span class="pun">):</span><span class="pln">
   dropdown_value </span><span class="pun">=</span><span class="pln"> form</span><span class="pun">.</span><span class="pln">getvalue</span><span class="pun">(</span><span class="str">'dropdown'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
   dropdown_value </span><span class="pun">=</span><span class="pln"> </span><span class="str">"没有内容"</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Content-type:text/html"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;html&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;meta charset=\"utf-8\"&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;title&gt;菜鸟教程 CGI 测试实例&lt;/title&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/head&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;h2&gt; 选中的选项是：%s&lt;/h2&gt;"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> dropdown_value</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/body&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"&lt;/html&gt;"</span><span class="pun">)</span></pre><p>修改 dropdown.py 权限：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">chmod </span><span class="lit">755</span><span class="pln"> dropdown</span><span class="pun">.</span><span class="pln">py</span></pre>
        <p>浏览器访问 Gif 演示图：</p>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/11/dropdown.gif"></p>

        <h2>CGI中使用Cookie</h2>
        <p>在 http 协议一个很大的缺点就是不对用户身份的进行判断，这样给编程人员带来很大的不便，
            而 cookie 功能的出现弥补了这个不足。</p><p>
        cookie 就是在客户访问脚本的同时，通过客户的浏览器，在客户硬盘上写入纪录数据
        ，当下次客户访问脚本时取回数据信息，从而达到身份判别的功能，cookie 常用在身份校验中。</p>
        　
        <h3>cookie的语法</h3>
        <p>http cookie的发送是通过http头部来实现的，他早于文件的传递，头部set-cookie的语法如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Set</span><span class="pun">-</span><span class="pln">cookie</span><span class="pun">:</span><span class="pln">name</span><span class="pun">=</span><span class="pln">name</span><span class="pun">;</span><span class="pln">expires</span><span class="pun">=</span><span class="pln">date</span><span class="pun">;</span><span class="pln">path</span><span class="pun">=</span><span class="pln">path</span><span class="pun">;</span><span class="pln">domain</span><span class="pun">=</span><span class="pln">domain</span><span class="pun">;</span><span class="pln">secure </span></pre>
        <ul>
            <li>

                <strong>name=name:</strong> 需要设置cookie的值(name不能使用"<strong>;</strong>"和"<strong>,</strong>"号),有多个name值时用 "<strong>;</strong>" 分隔，例如：<strong>name1=name1;name2=name2;name3=name3</strong>。 </li> <li>
            <strong>expires=date:</strong> cookie的有效期限,格式： expires="Wdy,DD-Mon-YYYY HH:MM:SS"</li> <li>
        </li> <li><strong>path=path: </strong>设置cookie支持的路径,如果path是一个路径，则cookie对这个目录下的所有文件及子目录生效，例如： path="/cgi-bin/"，如果path是一个文件，则cookie指对这个文件生效，例如：path="/cgi-bin/cookie.cgi"。
        </li> <li><strong>domain=domain:</strong> 对cookie生效的域名，例如：domain="www.runoob.com"
        </li> <li><strong>secure:</strong> 如果给出此标志，表示cookie只能通过SSL协议的https服务器来传递。
        </li> <li>cookie的接收是通过设置环境变量HTTP_COOKIE来实现的，CGI程序可以通过检索该变量获取cookie信息。</li></ul>
        <h2>Cookie设置 </h2>
        <p>Cookie的设置非常简单，cookie会在http头部单独发送。以下实例在cookie中设置了name 和 expires：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">
</span><span class="com"># </span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">'Content-Type: text/html'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">'Set-Cookie: name="菜鸟教程";expires=Wed, 28 Aug 2016 18:30:00 GMT'</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"""
&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset="</span><span class="pln">utf</span><span class="pun">-</span><span class="lit">8</span><span class="str">"&gt;
    &lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
  &lt;/head&gt;
    &lt;body&gt;
        &lt;h1&gt;Cookie set OK!&lt;/h1&gt;
    &lt;/body&gt;
&lt;/html&gt;
"""</span><span class="pun">)</span></pre><p>将以上代码保存到 cookie_set.py，并修改 cookie_set.py 权限：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">chmod </span><span class="lit">755</span><span class="pln"> cookie_set</span><span class="pun">.</span><span class="pln">py</span></pre>
        <p>以上实例使用了 Set-Cookie 头信息来设置Cookie信息，可选项中设置了Cookie的其他属性，如过期时间Expires，域名Domain，路径Path。这些信息设置在 "Content-type:text/html"之前。
        </p>

        <h2>
            检索Cookie信息</h2><p>
        Cookie信息检索页非常简单，Cookie信息存储在CGI的环境变量HTTP_COOKIE中，存储格式如下：</p>

        <pre class="prettyprint prettyprinted" style=""><span class="pln">key1</span><span class="pun">=</span><span class="pln">value1</span><span class="pun">;</span><span class="pln">key2</span><span class="pun">=</span><span class="pln">value2</span><span class="pun">;</span><span class="pln">key3</span><span class="pun">=</span><span class="pln">value3</span><span class="pun">....</span></pre>
        <p>以下是一个简单的CGI检索cookie信息的程序：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 导入模块</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> os
</span><span class="kwd">import</span><span class="pln"> http</span><span class="pun">.</span><span class="pln">cookies

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Content-type: text/html"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">()</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"""
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="</span><span class="pln">utf</span><span class="pun">-</span><span class="lit">8</span><span class="str">"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;读取cookie信息&lt;/h1&gt;
"""</span><span class="pun">)</span><span class="pln">

</span><span class="kwd">if</span><span class="pln"> </span><span class="str">'HTTP_COOKIE'</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> os</span><span class="pun">.</span><span class="pln">environ</span><span class="pun">:</span><span class="pln">
    cookie_string</span><span class="pun">=</span><span class="pln">os</span><span class="pun">.</span><span class="pln">environ</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="str">'HTTP_COOKIE'</span><span class="pun">)</span><span class="pln">
    c</span><span class="pun">=</span><span class="typ">Cookie</span><span class="pun">.</span><span class="typ">SimpleCookie</span><span class="pun">()</span><span class="pln">
    c</span><span class="pun">.</span><span class="pln">load</span><span class="pun">(</span><span class="pln">cookie_string</span><span class="pun">)</span><span class="pln">

    </span><span class="kwd">try</span><span class="pun">:</span><span class="pln">
        data</span><span class="pun">=</span><span class="pln">c</span><span class="pun">[</span><span class="str">'name'</span><span class="pun">].</span><span class="pln">value
        </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"cookie data: "</span><span class="pun">+</span><span class="pln">data</span><span class="pun">+</span><span class="str">"&lt;br&gt;"</span><span class="pun">)</span><span class="pln">
    </span><span class="kwd">except</span><span class="pln"> </span><span class="typ">KeyError</span><span class="pun">:</span><span class="pln">
        </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"cookie 没有设置或者已过去&lt;br&gt;"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"""
&lt;/body&gt;
&lt;/html&gt;
"""</span><span class="pun">)</span></pre><p>将以上代码保存到 cookie_get.py，并修改 cookie_get.py 权限：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">chmod </span><span class="lit">755</span><span class="pln"> cookie_get</span><span class="pun">.</span><span class="pln">py</span></pre>
        <p>以上 cookie 设置颜色 Gif 如下所示：</p>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/11/cookie.gif"></p>
        <h3>文件上传实例</h3>
        <p>HTML设置上传文件的表单需要设置 <strong>enctype</strong> 属性为 <strong>multipart/form-data</strong>，代码如下所示：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="dec">&lt;!DOCTYPE html&gt;</span><span class="pln">
</span><span class="tag">&lt;html&gt;</span><span class="pln">
</span><span class="tag">&lt;head&gt;</span><span class="pln">
</span><span class="tag">&lt;meta</span><span class="pln"> </span><span class="atn">charset</span><span class="pun">=</span><span class="atv">"utf-8"</span><span class="tag">&gt;</span><span class="pln">
</span><span class="tag">&lt;title&gt;</span><span class="pln">菜鸟教程(runoob.com)</span><span class="tag">&lt;/title&gt;</span><span class="pln">
</span><span class="tag">&lt;/head&gt;</span><span class="pln">
</span><span class="tag">&lt;body&gt;</span><span class="pln">
 </span><span class="tag">&lt;form</span><span class="pln"> </span><span class="atn">enctype</span><span class="pun">=</span><span class="atv">"multipart/form-data"</span><span class="pln">
                     </span><span class="atn">action</span><span class="pun">=</span><span class="atv">"/cgi-bin/save_file.py"</span><span class="pln"> </span><span class="atn">method</span><span class="pun">=</span><span class="atv">"post"</span><span class="tag">&gt;</span><span class="pln">
   </span><span class="tag">&lt;p&gt;</span><span class="pln">选中文件: </span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"file"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"filename"</span><span class="pln"> </span><span class="tag">/&gt;&lt;/p&gt;</span><span class="pln">
   </span><span class="tag">&lt;p&gt;&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"submit"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"上传"</span><span class="pln"> </span><span class="tag">/&gt;&lt;/p&gt;</span><span class="pln">
   </span><span class="tag">&lt;/form&gt;</span><span class="pln">
</span><span class="tag">&lt;/body&gt;</span><span class="pln">
</span><span class="tag">&lt;/html&gt;</span></pre>
        <p>save_file.py脚本文件代码如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> cgi</span><span class="pun">,</span><span class="pln"> os
</span><span class="kwd">import</span><span class="pln"> cgitb</span><span class="pun">;</span><span class="pln"> cgitb</span><span class="pun">.</span><span class="pln">enable</span><span class="pun">()</span><span class="pln">

form </span><span class="pun">=</span><span class="pln"> cgi</span><span class="pun">.</span><span class="typ">FieldStorage</span><span class="pun">()</span><span class="pln">

</span><span class="com"># 获取文件名</span><span class="pln">
fileitem </span><span class="pun">=</span><span class="pln"> form</span><span class="pun">[</span><span class="str">'filename'</span><span class="pun">]</span><span class="pln">

</span><span class="com"># 检测文件是否上传</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> fileitem</span><span class="pun">.</span><span class="pln">filename</span><span class="pun">:</span><span class="pln">
   </span><span class="com"># 设置文件路径 </span><span class="pln">
   fn </span><span class="pun">=</span><span class="pln"> os</span><span class="pun">.</span><span class="pln">path</span><span class="pun">.</span><span class="pln">basename</span><span class="pun">(</span><span class="pln">fileitem</span><span class="pun">.</span><span class="pln">filename</span><span class="pun">)</span><span class="pln">
   open</span><span class="pun">(</span><span class="str">'/tmp/'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> fn</span><span class="pun">,</span><span class="pln"> </span><span class="str">'wb'</span><span class="pun">).</span><span class="pln">write</span><span class="pun">(</span><span class="pln">fileitem</span><span class="pun">.</span><span class="pln">file</span><span class="pun">.</span><span class="pln">read</span><span class="pun">())</span><span class="pln">

   message </span><span class="pun">=</span><span class="pln"> </span><span class="str">'文件 "'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> fn </span><span class="pun">+</span><span class="pln"> </span><span class="str">'" 上传成功'</span><span class="pln">

</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
   message </span><span class="pun">=</span><span class="pln"> </span><span class="str">'文件没有上传'</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"""\
Content-Type: text/html\n
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="</span><span class="pln">utf</span><span class="pun">-</span><span class="lit">8</span><span class="str">"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
   &lt;p&gt;%s&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
"""</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="pun">(</span><span class="pln">message</span><span class="pun">,))</span></pre><p>将以上代码保存到 save_file.py，并修改 save_file.py 权限：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">chmod </span><span class="lit">755</span><span class="pln"> save_file</span><span class="pun">.</span><span class="pln">py</span></pre>
        <p>以上 cookie 设置颜色 Gif 如下所示：</p>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/11/savefile.gif"></p>
        <p>如果你使用的系统是Unix/Linux，你必须替换文件分隔符，在window下只需要使用open()语句即可：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">fn </span><span class="pun">=</span><span class="pln"> os</span><span class="pun">.</span><span class="pln">path</span><span class="pun">.</span><span class="pln">basename</span><span class="pun">(</span><span class="pln">fileitem</span><span class="pun">.</span><span class="pln">filename</span><span class="pun">.</span><span class="pln">replace</span><span class="pun">(</span><span class="str">"\\"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"/"</span><span class="pln"> </span><span class="pun">))</span></pre>
        <h2>文件下载对话框</h2>
        <p>我们先在当前目录下创建 foo.txt 文件，用于程序的下载。</p>
        <p>文件下载通过设置HTTP头信息来实现，功能代码如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># HTTP 头部</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Content-Disposition: attachment; filename=\"foo.txt\""</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">()</span><span class="pln">
</span><span class="com"># 打开文件</span><span class="pln">
fo </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"foo.txt"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"rb"</span><span class="pun">)</span><span class="pln">

str </span><span class="pun">=</span><span class="pln"> fo</span><span class="pun">.</span><span class="pln">read</span><span class="pun">();</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="pln">str</span><span class="pun">)</span><span class="pln">

</span><span class="com"># 关闭文件</span><span class="pln">
fo</span><span class="pun">.</span><span class="pln">close</span><span class="pun">()</span></pre>

    </div>
</div>
</body>
</html>
